<%--
  Created by IntelliJ IDEA.
  User: cjbi
  Date: 2018/4/6
  Time: 7:43
  空页面.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<style>
    .container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #f9f9f9;
    }
    h1 {
    text-align: center;
    color: #333;
    }
    label {
    display: block;
    margin: 10px 0 5px;
    font-weight: bold;
    }
    input[type="text"], input[type="password"], select, textarea {
    width: 100%;
    padding: 8px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    }
    button {
    display: block;
    width: 100%;
    padding: 10px;
    background-color: #28a745;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    }
    button:hover {
    background-color: #218838;
    }
    .form-group {
    margin-bottom: 15px;
    }
    .dynamic-field {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    }
    .dynamic-field input {
    flex: 1;
    }
    .dynamic-field button {
    width: auto;
    padding: 5px 10px;
    background-color: #dc3545;
    }
    .dynamic-field button:hover {
    background-color: #c82333;
    }
</style>
<!-- Content Header (Page header) -->
<section class="content-header" style="">
    <h1>
        <small>代码生成器</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
        <li><a href="#">代码生成器</a></li>
        <li class="active">代码生成器</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <!-- Default box -->
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title"><%--代码生成器功能开发中...--%></h3>

            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" id="collapseButton" data-widget="collapse" data-toggle="collapse" data-target="#panelCollapse"
                        title="折叠">
                    <i class="fa fa-plus"></i></button><!-- 初始状态为加号 -->
            </div>
        </div>

        <div class="box-body">
            <div class="container">
               <%-- <h1>MyBatis Generator</h1>--%>
                <form id="generatorForm">
                    <!-- 项目配置 -->
                    <div class="form-group">
                        <label for="projectFolder">项目文件夹:</label>
                        <input type="text" id="projectFolder" name="projectFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="moduleName">模块名称:</label>
                        <input type="text" id="moduleName" name="moduleName" required>
                    </div>

                    <div class="form-group">
                        <label for="mappingXMLTargetFolder">XML文件夹:</label>
                        <input type="text" id="mappingXMLTargetFolder" name="mappingXMLTargetFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="mappingXMLPackage">XML包名:</label>
                        <input type="text" id="mappingXMLPackage" name="mappingXMLPackage" required>
                    </div>

                    <div class="form-group">
                        <label for="daoTargetFolder">DAO接口目标目录:</label>
                        <input type="text" id="daoTargetFolder" name="daoTargetFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="daoPackage">DAO 包名:</label>
                        <input type="text" id="daoPackage" name="daoPackage" required>
                    </div>

                    <div class="form-group">
                        <label for="modelPackageTargetFolder">Model类目标目录:</label>
                        <input type="text" id="modelPackageTargetFolder" name="modelPackageTargetFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="modelPackage">Model 包名:</label>
                        <input type="text" id="modelPackage" name="modelPackage" required>
                    </div>

                    <div class="form-group">
                        <label for="modelName">Model 名称:</label>
                        <input type="text" id="modelName" name="modelName">
                    </div>

                    <!-- Service接口目标路径 -->
                    <div class="form-group">
                        <label for="serviceTargetFolder">Service接口目标目录:</label>
                        <input type="text" id="serviceTargetFolder" name="serviceTargetFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="servicePackage">Service 包名:</label>
                        <input type="text" id="servicePackage" name="servicePackage" required>
                    </div>

                    <div class="form-group">
                        <label for="serviceName">Service接口名称:</label>
                        <input type="text" id="serviceName" name="serviceName"
                               class="form-control">
                    </div>

                    <!-- Service实现目标路径 -->
                    <div class="form-group">
                        <label for="serviceImplTargetFolder">Service实现目标目录:</label>
                        <input type="text" id="serviceImplTargetFolder" name="serviceImplTargetFolder" required>
                    </div>

                    <!-- 新增服务实现层配置 -->
                    <div class="form-group">
                        <label for="serviceImplName">Service实现类名称:</label>
                        <input type="text" id="serviceImplName" name="serviceImplName"
                               class="form-control">
                    </div>

                    <div class="form-group">
                        <label for="serviceImplPackage">Service实现包名:</label>
                        <input type="text" id="serviceImplPackage" name="serviceImplPackage"
                               class="form-control" required>
                    </div>

                    <!-- Controller目标路径 -->
                    <div class="form-group">
                        <label for="controllerTargetFolder">Controller目标目录:</label>
                        <input type="text" id="controllerTargetFolder" name="controllerTargetFolder" required>
                    </div>

                    <div class="form-group">
                        <label for="controllerPackage">Controller 包名:</label>
                        <input type="text" id="controllerPackage" name="controllerPackage" required>
                    </div>

                    <div class="form-group">
                        <label for="controllerName">Controller名称:</label>
                        <input type="text" id="controllerName" name="controllerName">
                    </div>

                    <!-- 添加JSP目标路径配置 -->
                    <div class="form-group">
                        <label for="jspTargetFolder">JSP目标目录:</label>
                        <input type="text" id="jspTargetFolder" name="jspTargetFolder"
                               placeholder="/WEB-INF/views" class="form-control" required>
                    </div>

                    <!-- 表配置 -->
                    <div class="form-group">
                        <label for="tableName">表名:</label>
                        <%--<input type="text" id="tableName" name="tableName" required>--%>
                        <select name="tableName" id="tableName"  class="form-control chosen-select"
                                data-placeholder="请从列表选择一项" required>
                            <c:forEach items="${tableNames}" var="tableName">
                                <option value="${tableName}">${tableName}</option>
                            </c:forEach>
                        </select>
                    </div>

                    <div id="panelCollapse" class="panel-collapse collapse">
                    <!-- 生成选项 -->
                    <div class="form-group">
                        <label>
                            <input type="checkbox" id="offsetLimit" name="offsetLimit"> 启用分页
                        </label>
                    </div>
                    <div class="form-group">
                        <label>
                            <input type="checkbox" id="comment" name="comment"> 生成注释
                        </label>
                    </div>
                    <div class="form-group">
                        <label>
                            <input type="checkbox" id="annotation" name="annotation"> 启用注解
                        </label>
                    </div>

                    <!-- 忽略列 -->
                    <div class="form-group">
                        <label>忽略列:</label>
                        <div id="ignoredColumnsContainer">
                            <div class="dynamic-field">
                                <input type="text" name="ignoredColumns" placeholder="列名">
                                <button type="button" onclick="removeField(this)">删除</button>
                            </div>
                        </div>
                        <button type="button" onclick="addIgnoredColumn()">添加忽略列</button>
                    </div>

                    <!-- 列覆盖 -->
                    <div class="form-group">
                        <label>列覆盖:</label>
                        <div id="columnOverridesContainer">
                            <div class="dynamic-field">
                                <input type="text" name="columnOverrides" placeholder="列名">
                                <input type="text" name="columnOverrides" placeholder="属性名">
                                <button type="button" onclick="removeField(this)">删除</button>
                            </div>
                        </div>
                        <button type="button" onclick="addColumnOverride()">添加列覆盖</button>
                    </div>
                    </div>
                    <!-- 提交按钮 -->
                    <button type="submit">生成代码</button>
                </form>
            </div>
        </div>
        <!-- /.box-body -->

        <div class="box-footer">
            <%--代码生成器功能开发中...--%>
        </div>
        <!-- /.box-footer-->
    </div>
    <!-- /.box -->

</section>
<!-- /.content -->

<script>
    // 动态添加忽略列
    function addIgnoredColumn() {
        const container = document.getElementById('ignoredColumnsContainer');
        const div = document.createElement('div');
        div.className = 'dynamic-field';
        div.innerHTML = `
                <input type="text" name="ignoredColumns" placeholder="列名">
                <button type="button" onclick="removeField(this)">删除</button>
            `;
        container.appendChild(div);
    }

    // 动态添加列覆盖
    function addColumnOverride() {
        const container = document.getElementById('columnOverridesContainer');
        const div = document.createElement('div');
        div.className = 'dynamic-field';
        div.innerHTML = `
                <input type="text" name="columnOverrideColumns" placeholder="列名">
                <input type="text" name="columnOverrideProperties" placeholder="属性名">
                <button type="button" onclick="removeField(this)">删除</button>
            `;
        container.appendChild(div);
    }

    // 删除动态字段
    function removeField(button) {
        button.parentElement.remove();
    }

    // 提交表单
    document.getElementById('generatorForm').addEventListener('submit', async (e) => {
        e.preventDefault();

        const formData = new FormData(e.target);
        const data = {};
        formData.forEach((value, key) => {
            console.log(key, value); // 调试输出
            if (key === 'ignoredColumns' || key === 'columnOverrides') {
                if (!data[key]) data[key] = [];
                data[key].push(value);
            } else {
                data[key] = value;
            }
        });
        // 调试输出完整数据
        console.log("提交的数据:", JSON.stringify(data, null, 2));
        try {
            const response = await fetch('${path}/generator/generate', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data),
            });

            if (response.ok) {
                const blob = await response.blob();
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'generated_code.zip';
                a.click();
                window.URL.revokeObjectURL(url);
            } else {
                alert('代码生成失败，请检查输入参数！');
            }
        } catch (error) {
            console.error('Error:', error);
            alert('代码生成失败，请检查输入参数！');
        }
    });

    $(document).ready(function() {
        $('#collapseButton').click(function() {
            $(this).find('i').toggleClass('fa-plus fa-minus');
        });
    });

</script>